﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.EntityClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FaWorks.WebSite.Controllers.Model
{
    public class UserController : Controller
    {
        [ActionName("CURD"), HttpGet]
        public ActionResult Read(int? id)
        {
            //判断是查询单条
            if (id == null){
                string curPage = HttpContext.Request.QueryString["page"];
                if (String.IsNullOrEmpty(curPage))
                {
                    curPage = "1";
                }

                if (curPage == "GetRecordCount") {
                    return JavaScript("{ \"recordCount\": \"" + FaDB.Users.Count() + "\" }");
                }

                int pageNum = Int32.Parse(curPage);

                var models =
                    (from u in FaDB.Users
                     orderby u.id
                     select u).Skip((pageNum - 1) * 20).Take(20);
                return Json(models, JsonRequestBehavior.AllowGet);
            } else {
                return Json(FaDB.Users.Find(id.Value), JsonRequestBehavior.AllowGet);
            }
        }

        [ActionName("CURD"), HttpPost]
        public ActionResult Create(Models.User user) {
            Models.AjaxResult ar = new Models.AjaxResult();
            try {
                FaDB.Users.Add(user);
                FaDB.SaveChanges();

                Hubs.UserHub.Instance.Clients.All.Create(user);
            }
            catch (Exception ex) {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            ar.Data = user.id;
            return Json(ar);
        }

        [ActionName("CURD"), HttpPut]
        public ActionResult Update(Models.User user) {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                
                var curUser = FaDB.Users.Find(user.id);
                curUser.UserName = user.UserName;
                curUser.Password = user.Password;
                curUser.Email = user.Email;
                FaDB.SaveChanges();

                Hubs.UserHub.Instance.Clients.All.Update(curUser);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            return Json(ar);
        }

        [ActionName("CURD"), HttpDelete]
        public ActionResult Delete(int id) {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                var curUser = FaDB.Users.Find(id);
                FaDB.Users.Remove(curUser);
                FaDB.SaveChanges();

                Hubs.UserHub.Instance.Clients.All.Delete(curUser);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            return Json(ar);
        }


        FaDataBase FaDB = new FaDataBase();

    }
}
